package com.smarttiger.zero.admin.webservice;

import javax.xml.datatype.DatatypeConfigurationException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zhouzhengwei on 2021/11/16.
 */
public class TransferUtil {

    public static Object transferJ2W(String className,Object datatmp){
        if("java.util.Date".equals(className)){
            Date date = (Date)datatmp;

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            javax.xml.datatype.DatatypeFactory dtf = null;
            try {
                dtf = javax.xml.datatype.DatatypeFactory.newInstance();
            } catch (DatatypeConfigurationException e) {
                e.printStackTrace();
            }
            return dtf.newXMLGregorianCalendar(calendar.get(calendar.YEAR),
                    calendar.get(calendar.MONTH) + 1, calendar
                            .get(calendar.DAY_OF_MONTH), calendar
                            .get(calendar.HOUR), calendar.get(calendar.MINUTE),
                    calendar.get(calendar.SECOND), calendar
                            .get(calendar.MILLISECOND), calendar
                            .get(calendar.ZONE_OFFSET)
                            / (1000 * 60));
        }else {
            return datatmp;
        }
    }


    public static Object transferTaskJ2W(String className,Object data){
        if("java.util.Date".equals(className)){
            Date date = null;
            try {
                if (data.toString().trim().length() > 10) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    date = sdf.parse(data.toString());
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    date = sdf.parse(data.toString());
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            javax.xml.datatype.DatatypeFactory dtf = null;
            try {
                dtf = javax.xml.datatype.DatatypeFactory.newInstance();
            } catch (DatatypeConfigurationException e) {
                e.printStackTrace();
            }
            return dtf.newXMLGregorianCalendar(calendar.get(calendar.YEAR),
                    calendar.get(calendar.MONTH) + 1, calendar
                            .get(calendar.DAY_OF_MONTH), calendar
                            .get(calendar.HOUR), calendar.get(calendar.MINUTE),
                    calendar.get(calendar.SECOND), calendar
                            .get(calendar.MILLISECOND), calendar
                            .get(calendar.ZONE_OFFSET)
                            / (1000 * 60));
        }else if("java.math.BigDecimal".equals(className)){
            return new BigDecimal(data.toString());
        }else if("java.lang.Integer".equals(className)){
            return Integer.valueOf(data.toString());
        }else if("java.lang.Double".equals(className)){
            return Double.valueOf(data.toString());
        }else if("java.lang.String".equals(className)){
            return data;
        }else{
            return null;
        }
    }

    public static Object transferS2J(String className,String data){
        if("java.math.BigDecimal".equals(className)){
            return new BigDecimal(data.toString());
        }else if("java.util.Date".equals(className)){
            Date date = null;
            try {
                if (data.toString().trim().length() > 10) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    date = sdf.parse(data.toString());
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    date = sdf.parse(data.toString());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return date;
        }else if("java.lang.Integer".equals(className)){
            return Integer.valueOf(data.toString());
        }else if("java.lang.Double".equals(className)){
            return Double.valueOf(data.toString());
        }else {
            return data;
        }
    }
}
